// index.js
const util = require("../../utils/request");
Page({
  data: {
    dishes: [], // 本次下单情况
    show: false, // 显示抽屉
    price: 0, // 本次订单总价格
    isMember: false, // 是否为会员
    member: {}, // 会员信息
    originPrice: 0, // 未打折的原始价格
    score: 0, // 订单评分
    showDialog: false // 显示确认弹框
  },

   /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getMemberByWxId();
    this.getDishesByOrderId();
  },

  /**
   * 查询会员信息
   */
  getMemberByWxId() {
    const openid = wx.getStorageSync('openid');
    util.request("/rms/member/getInfoByWxId/" + openid, 'GET', {}, res => {
        let code = res.data.code;
        if(code == 200) {
            let member = res.data.data;
            if(member == null) {
                this.setData({isMember: false});
            } else {
                this.setData({
                    isMember: true,
                    member: member
                });
            }
        }
    });
  },

  /**
   * 根据订单查询下单信息
   */
  getDishesByOrderId() {
    const orderId = wx.getStorageSync('orderId');
    let originPrice = 0;
    if(!orderId) return;
    util.request('/rms/dishesOrder/selectDishesByOrderId/' + orderId, 'GET', {}, res => {
        let dishes = res.data.data;
        if(dishes != null && dishes.length > 0) {
            for(let index in dishes) {
                dishes[index].pictureUrl = 'http://localhost:8091/profile/upload' + dishes[index].pictureUrl
                originPrice += dishes[index].price * dishes[index].orderNum;
            }
            this.setData({
                dishes: dishes,
                originPrice: originPrice
            });
            if(this.data.member) {
                this.setData({price: originPrice * this.data.member.discount});
            } else {
                this.setData({price: originPrice});
            }
        }
    });
  },

  /**
   * 查看详情按钮触发函数
   */
  getDetail() {
    this.getMemberByWxId();
    this.getDishesByOrderId();
    this.setData({ show: true });
  },

  /**
   * 结账按钮
   */
  onClickEnsureButton() {
     this.setData({
         showDialog: true
     });
  },

  /**
   * 捕获评分值
   */
  onChange(event) {
      this.setData({
          score: event.detail
      });
  },

  /**
   * 取消提交结账
   */
  cancelSubmit() {
      this.setData({
        showDialog: false,
        score: 0
      });
  },

  /**
   * 确认结账
   */
  onClickSubmitButton() {
      const orderId = wx.getStorageSync('orderId');
      let score = this.data.score;
      if(score == 0) return;
      util.request('/rms/order/checkOut/' + orderId + '/' + score, 'PUT', {}, res => {
          let code = res.data.code;
          if(code == 200) {
            wx.removeStorageSync('orderId');
            wx.removeStorageSync('seatId');
            wx.removeStorageSync('realAccount');
          }
          this.reset();
          this.cancelSubmit();
      });
  },

  /**
   * 关闭抽屉
   */
  onClose() {
    this.setData({ show: false });
  },

  /**
   * 充值按钮
   */
  onRecharge() {
    this.setData({
        show: false
    });
    wx.redirectTo({
        url: '../me/me',
    });
  },
  reset() {
      this.setData({
          dishes: [],
          price: 0,
          originPrice: 0,
          show: false
      })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
   
  }
})
